package org.springmvc.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springmvc.model.User;
import org.springmvc.service.UserService;

public class DataInitListener implements ServletContextListener {

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		WebApplicationContext application = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
		UserService userService = application.getBean(UserService.class);
		String key = "admin";
		if (userService.getUser(key) == null) {
			User user = new User();
			user.setName(key).setPassword(key).setSex(true);
			userService.createOrUpdate(user);
		}
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
	}

}
